package com.jnc.web.biz.system.controller;

import com.jnc.common.base.BaseResp;
import com.jnc.common.constant.SysConstant;
import com.jnc.core.base.controller.BaseController;
import com.jnc.core.base.model.Menu;
import com.jnc.core.base.model.User;
import com.jnc.core.base.service.MenuService;
import com.jnc.core.util.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: jjn
 * @Date: 2018/10/11
 * @Desc:
 */
@Slf4j
@Controller
public class LoginController extends BaseController {

    @Autowired
    private MenuService menuService;

    /**
     * 会话过期，跳转登录界面
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response){
        //如果是ajax请求，返回json字符串
        if(ServletUtil.isAjaxRequest(request)){
            BaseResp result = new BaseResp();
            result.setCode(SysConstant.STATUS_CODE_ERROR).setMsg("未登录或登录超时，请重新登录");
            return ServletUtil.responseResult(response, result);
        }
        return "login";
    }

    /**
     * 跳转登录界面
     * @return
     */
    @GetMapping("/")
    public String login(){
        return "login";
    }

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public BaseResp ajaxLogin(@RequestBody User user){
        UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginName().trim(), user.getPassword().trim(), user.getRememberMe());
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(token);
            return success("登录成功");
        }catch (UnknownAccountException e){
            return error("用户不存在");
        }catch (LockedAccountException e){
            return error("账号已被锁定，请联系管理员");
        }catch (AuthenticationException e){
            return error("用户名和密码错误");
        }
    }

    /**
     * 系统首页
     * @param model
     * @return
     */
    @GetMapping("/index")
    public String index(ModelMap model){
        //获取用户信息
        User user = getUser();
        //根据用户id取出菜单
        List<Menu> menus = menuService.queryMenusByUser(user);
        model.put("menus", menus);
        model.put("user", user);
        return "index";
    }

    /**
     * 欢迎页面
     * @param mmap
     * @return
     */
    @GetMapping("/system/main")
    public String main(ModelMap mmap){
        return "main";
    }


    /**
     * 权限认证不通过，跳转没有权限界面
     * @return
     */
    @GetMapping("/unauth")
    public String unauth(){
        return "/error/403";
    }

}
